package com.bryantkobe.myspringcode.service.impl;

import com.bryantkobe.myspringcode.service.MExceptionMetricsService;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 性能监控服务实现类
 * @author: alienwarezzl
 * @date: 2025/9/23
 */
@Service
public class MExceptionMetricsServiceImpl implements MExceptionMetricsService {

    private final MeterRegistry meterRegistry;

    private final Map<String, Counter> exceptionCounters = new ConcurrentHashMap<>();

    public MExceptionMetricsServiceImpl(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }

    @Override
    public void recordException(String exceptionType, String errorCode) {
        String counterName = String.format("exception:%s,errorCode:%s", exceptionType, errorCode);
        Counter counter = exceptionCounters.computeIfAbsent(counterName,
                key -> Counter.builder(key)
                        .description("出现异常")
                        .register(meterRegistry));
        counter.increment();
    }
}
